﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Web.Views.DataManage.Models;
using System.Web.Script.Serialization;
using ePlanet.SingleWell.DataAccess;
using ePlanet.SingleWell.Models;
using System.Data;
using System.Web.Script.Services;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.DataAccess;
using ePlanet.Entities;

namespace Web.Views.DataManage
{
    /// <summary>
    /// DataManageServices 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class DataManageServices : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string UpdateSingleWellData(string updateModel)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                SingleWellModel sModel = new SingleWellModel();
                SingleWellDAl singleWellDAL = new SingleWellDAl();
                DatasUpdateModel<SingleWellEntity> model = jss.Deserialize<DatasUpdateModel<SingleWellEntity>>(updateModel);
                foreach (SingleWellEntity entity in model.InsertedRows)
                {
                    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                    SingleWellEntityHelper.EntityToRow(entity, row);
                    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                }
                foreach (SingleWellEntity entity in model.UpdatedRows)
                {
                    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                    SingleWellEntityHelper.EntityToRow(entity, row);
                    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                }
                foreach (SingleWellEntity entity in model.DeletedRows)
                {
                    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                    SingleWellEntityHelper.EntityToRow(entity, row);
                    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                }
                sModel.SINGLEWELL.AcceptChanges();
                foreach (SingleWellModel.SINGLEWELLRow row in sModel.SINGLEWELL.Rows)
                {
                    foreach (SingleWellEntity entity in model.InsertedRows)
                    {
                        if (entity.DataId == row.DATAID)
                        {
                            row.SetAdded();
                            goto NextStep;
                        }
                    }

                    foreach (SingleWellEntity entity in model.UpdatedRows)
                    {
                        if (entity.DataId == row.DATAID)
                        {
                            row.SetModified();
                            goto NextStep;
                        }
                    }

                    foreach (SingleWellEntity entity in model.DeletedRows)
                    {
                        if (entity.DataId == row.DATAID)
                        {
                            row.Delete();
                            goto NextStep;
                        }
                    }
                    NextStep:
                    {
                        continue;
                    }
                }
                singleWellDAL.AddNewSingleWellModel(sModel);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return jss.Serialize(new { responseStatus = "Success", Message = "Success" });
        }

        [WebMethod]
        public string UpdataArchiveType(string updateModel)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                ArchiveMangeModal archiveMangeModal = new ArchiveMangeModal();
                ArchiveMenuDAL archiveMenuDAL = new ArchiveMenuDAL();
                ArchiveTypeUpdateModel model = jss.Deserialize<ArchiveTypeUpdateModel>(updateModel);
                foreach (ArchiveTypeViewModel archiveTypeViewModel in model.InsertedRows)
                {
                    ArchiveMangeModal.ARCHIVEMENURow row = archiveMangeModal.ARCHIVEMENU.NewARCHIVEMENURow();
                    row = ArchiveTypeViewModel.ViewToArchiveMenuRow(archiveTypeViewModel, row);
                    archiveMangeModal.ARCHIVEMENU.AddARCHIVEMENURow(row);
                }
                foreach (ArchiveTypeViewModel archiveTypeViewModel in model.UpdatedRows)
                {
                    ArchiveMangeModal.ARCHIVEMENURow row = archiveMangeModal.ARCHIVEMENU.NewARCHIVEMENURow();
                    row = ArchiveTypeViewModel.ViewToArchiveMenuRow(archiveTypeViewModel, row);
                    archiveMangeModal.ARCHIVEMENU.AddARCHIVEMENURow(row);
                }
                foreach (ArchiveTypeViewModel archiveTypeViewModel in model.DeletedRows)
                {
                    ArchiveMangeModal.ARCHIVEMENURow row = archiveMangeModal.ARCHIVEMENU.NewARCHIVEMENURow();
                    row = ArchiveTypeViewModel.ViewToArchiveMenuRow(archiveTypeViewModel, row);
                    archiveMangeModal.ARCHIVEMENU.AddARCHIVEMENURow(row);
                }
                archiveMangeModal.ARCHIVEMENU.AcceptChanges();
                foreach (ArchiveMangeModal.ARCHIVEMENURow row in archiveMangeModal.ARCHIVEMENU.Rows)
                {
                    foreach (ArchiveTypeViewModel archiveTypeViewModel in model.InsertedRows)
                    {
                        if (archiveTypeViewModel.TypeID == row.TYPEID)
                        {
                            row.SetAdded();
                            goto NextStep;
                        }
                    }
                    foreach (ArchiveTypeViewModel archiveTypeViewModel in model.UpdatedRows)
                    {
                        if (archiveTypeViewModel.TypeID == row.TYPEID)
                        {
                            row.SetModified();
                            goto NextStep;
                        }
                    }
                    foreach (ArchiveTypeViewModel archiveTypeViewModel in model.DeletedRows)
                    {
                        if (archiveTypeViewModel.TypeID == row.TYPEID)
                        {
                            row.Delete();
                            goto NextStep;
                        }
                    }
                    NextStep:
                    continue;
                }
                archiveMenuDAL.SaveArchiveMenu(archiveMangeModal);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return jss.Serialize(new { responseStatus = "Success", Message = "Success" });
        }

        [WebMethod]
        public string UpdataWellCatalog(string updateModel)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<string> DataIds = new List<string>();
            try
            {
                WellCatalog wellCatalog = new WellCatalog();
                WellCatalogDAL wellCatalogDAL = new WellCatalogDAL();
                WellCatalogUpdateModel model = jss.Deserialize<WellCatalogUpdateModel>(updateModel);
                foreach (WellCatalogViewModel wellCatalogViewModel in model.InsertedRows)
                {
                    WellCatalog.WELLCATALOGRow row = wellCatalog.WELLCATALOG.NewWELLCATALOGRow();
                    wellCatalogViewModel.WellcatalogId = string.Concat(Guid.NewGuid());
                    DataIds.Add(wellCatalogViewModel.WellcatalogId);
                    row = WellCatalogViewModel.ViewToWellCatalogRow(wellCatalogViewModel, row);
                    wellCatalog.WELLCATALOG.AddWELLCATALOGRow(row);
                }
                foreach (WellCatalogViewModel wellCatalogViewModel in model.UpdatedRows)
                {
                    WellCatalog.WELLCATALOGRow row = wellCatalog.WELLCATALOG.NewWELLCATALOGRow();
                    row = WellCatalogViewModel.ViewToWellCatalogRow(wellCatalogViewModel, row);
                    wellCatalog.WELLCATALOG.AddWELLCATALOGRow(row);
                }
                foreach (WellCatalogViewModel wellCatalogViewModel in model.DeletedRows)
                {
                    WellCatalog.WELLCATALOGRow row = wellCatalog.WELLCATALOG.NewWELLCATALOGRow();
                    row = WellCatalogViewModel.ViewToWellCatalogRow(wellCatalogViewModel, row);
                    wellCatalog.WELLCATALOG.AddWELLCATALOGRow(row);
                }
                wellCatalog.WELLCATALOG.AcceptChanges();
                foreach (WellCatalog.WELLCATALOGRow row in wellCatalog.WELLCATALOG.Rows)
                {
                    foreach (WellCatalogViewModel wellCatalogViewModel in model.InsertedRows)
                    {
                        if (wellCatalogViewModel.WellcatalogId == row.WELLCATALOGID)
                        {
                            row.SetAdded();
                            goto NextStep;
                        }
                    }
                    foreach (WellCatalogViewModel wellCatalogViewModel in model.UpdatedRows)
                    {
                        if (wellCatalogViewModel.WellcatalogId == row.WELLCATALOGID)
                        {
                            row.SetModified();
                            goto NextStep;
                        }
                    }
                    foreach (WellCatalogViewModel wellCatalogViewModel in model.DeletedRows)
                    {
                        if (wellCatalogViewModel.WellcatalogId == row.WELLCATALOGID)
                        {
                            row.Delete();
                            goto NextStep;
                        }
                    }
                NextStep:
                    continue;
                }
                if (model.DeletedRows.Count > 0)
                {
                    wellCatalogDAL.DeleteWellCatalogAll(model.DeletedRows[0].WellcatalogId);
                }
                else
                {
                    wellCatalogDAL.AddNewCatalog(wellCatalog);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return jss.Serialize(new { responseStatus = "Success", Message = "Success", DataIds = DataIds });
        }

        [WebMethod]
        public string IsEmptyIsThisWellCatalog(string catalogId)
        {
            try
            {
                /*WellCatalogDAL wellCatalogDAL = new WellCatalogDAL();
                WellCatalog wellCatalog = wellCatalogDAL.GetWellCatalogByID(catalogId);
                if (wellCatalog == null)
                {
                    throw new Exception("不存在Id为"+catalogId+"的编目");
                }*/
                ePlanet.SingleWell.DataAccess.SingleWellDAl singleWellDAl = new ePlanet.SingleWell.DataAccess.SingleWellDAl();
                int count = singleWellDAl.GetSingleWellCount(catalogId);
                if (count > 0)
                {
                    return "0";
                }
                else
                {
                    return "1";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [WebMethod]
        public string IsEmptyInThisArchiveType(string typeId)
        {
            try
            {
                return "1";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
